//
//  MyCollectionViewController.m
//  ioshuanwu
//
//  Created by 殷殷明静 on 16/9/19.
//  Copyright © 2016年 幻音. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "DetailCollectViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "YMJMacro_h.h"
#import "DataModel.h"
#import "Model.h"
#import "NewsSummaryInfo.h"
#import "HomeTableViewCell.h"
#import "HomeTableViewSecondCell.h"
@interface MyCollectionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *editBtn;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation MyCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self initNavigationLeftButton];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createView];
    [self createData];
}
- (void)createView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"1"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeTableViewSecondCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"2"];
}
- (void)createData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    [manager.requestSerializer setValue:@"Dalvik/1.6.0 (Linux; U; Android 4.2.1; R823T Build/JOP40D) Liuda/2.2.0 (agent:s;channel:360;credential:e1MxMDIwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzg2MzMzMTAyODY3NDU0Mn0teyMjIyMjIyMxNDczNTc0MjMyMDA3LTFhNjJjMmEzYzB9QDE0NzQxMDAzNzUyODI=;deviceId:863331028674542;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:4.2.1;idfa:null)" forHTTPHeaderField:@"User-Agent"];
//    [manager.requestSerializer setValue:@"api.9liuda.com" forHTTPHeaderField:@"Host"];
    
    [manager GET:@"http://api.9liuda.com/v1/news/collect/list" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
    
        Model *mo = [Model modelWithDic:dic];
    
        _dataArray = [NSMutableArray arrayWithArray:mo.data];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    DataModel *data = _dataArray[indexPath.row];
    NewsSummaryInfo *info = [data.newsSummaryInfoList firstObject];

    if (info.newsImageInfoList.count == 2) {
        return 180;
    } else {
        return 76;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DataModel *data = _dataArray[indexPath.row];
   NewsSummaryInfo *info = [data.newsSummaryInfoList lastObject];
    
    if (info.newsImageInfoList.count == 2) {
    
        HomeTableViewSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"2"];
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:[info.newsImageInfoList firstObject].url]];
        cell.myTitleLabel.text = info.title;
        cell.myOriginLabel.text = info.origin;
        return cell;
    }
    else {
        HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:[info.newsImageInfoList firstObject].url]];
        cell.myTitleLabel.text = info.title;
        cell.myOriginLabel.text = info.origin;
        return cell;
    }
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DataModel *data = _dataArray[indexPath.row];
    NewsSummaryInfo *info = [data.newsSummaryInfoList lastObject];
    DetailCollectViewController *detailVC = [DetailCollectViewController new];
    detailVC.detailLink = info.detailLink;
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:detailVC animated:YES];
    [self setHidesBottomBarWhenPushed:NO];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)initNavigationLeftButton {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 25)];
    ;
    label.text = @"我的收藏";
    label.font = [UIFont systemFontOfSize:13];
    self.navigationItem.titleView = label;
    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_backBtn];
    _backBtn.frame = CGRectMake(20, 20, 15, 15);
    [_backBtn setBackgroundImage:[UIImage imageNamed:@"Icon_jiantou"] forState:0];
    [_backBtn addTarget:self action:@selector(backToMyViewController) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_backBtn];
    /*编辑*/
    _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _editBtn.frame = CGRectMake(20, 20, 40, 20);
    [_editBtn setTitle:@"编辑" forState:0];
    [_editBtn setTitleColor:[UIColor blackColor] forState:0];
    _editBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_editBtn];
    [_editBtn addTarget:self action:@selector(clearRestore:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)clearRestore:(UIButton *)btn {
    
}
// 返回之前页面
- (void)backToMyViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
